package com.utils.basehelper.date;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;

public class DateUtil2 {
	
	/**
	 * 根据季度返回第几月
	 * @param quarter 季度
	 * @param num  月份
	 * @return
	 */
	public static String getMonthByQuarter(int quarter, int num) {
		String month = quarter*3-(3-num)+"";
		if(month.length()==1) 
			month="0"+month;
		return month;
	}
	
	/**
	 * 根据季度返回日期
	 * @param quarter 季度
	 * @param num  月份
	 * @return
	 */
	public static String getMonthByQuarter(String year, int quarter, int num) {
		String month = quarter*3-(3-num)+"";
		if(month.length()==1) 
			month="0"+month;
		return year+"-"+month;
	}
	
	/**
	 * 获取查询日期所属季度的剩余天数
	 * @param finalDate 账号季度最后消耗日期
	 * @param year 查询年份
	 * @param month 查询月份
	 * @param quarter 查询季度
	 * @return
	 */
	public static int getDays(String year, String month, int quarter) {
		int days = 0;
		int nowYear = DateTime.now().getYear();
		int nowMonth = DateTime.now().getMonthOfYear();
		int yearNum = 0;
		int monthNum = 0;
		try {
			yearNum = Integer.parseInt(year);
			monthNum = Integer.parseInt(month);
		} catch (Exception e) {
			throw new IllegalArgumentException("The parameter must be a Number");
		}
		if(nowYear>yearNum) {
			days = 0;
		}
		else if(nowYear==yearNum) {
			if(getQuarter(nowMonth)>getQuarter(monthNum)) {
				days = 0;
			}else if(getQuarter(nowMonth)<getQuarter(monthNum)) {
				days = getDayOfQuarter(nowYear, quarter);
			}else {
				days = getRemainDayOfQuarter();
			}
		}else {
			days = getDayOfQuarter(yearNum, quarter);
		}
		
		return days;
	}
	
	/**
	 * 根据月份查询对应季度
	 * @param month
	 * @return
	 */
	public static int getQuarter(int month) {
		int quarter = 0;
		switch(month){
			case 1:
			case 2:
			case 3:
				quarter=1;
				break;
			case 4:
			case 5:
			case 6:
				quarter=2;
				break;
			case 7:
			case 8:
			case 9:
				quarter=3;
				break;
			case 10:
			case 11:
			case 12:
				quarter=4;
				break;
			default:
				throw new IllegalArgumentException("The month must between 1 and 12");
		}
		return quarter;
	}
	
	/**
	 * 取得季度全部天数
	 * @param date
	 * @return
	 */
	public static int getDayOfQuarter(int year,int quarter) {
		int day = 0;
		for (int i = 1; i < 4; i++) {
			Calendar a = Calendar.getInstance();
			a.set(Calendar.YEAR, year);
			a.set(Calendar.MONTH, quarter * 3 - (3 - i) - 1);
			a.set(Calendar.DATE, 1);
			a.roll(Calendar.DATE, -1);
			day = a.get(Calendar.DATE) + day;
		}
		return day;
	}
	
	/**
	 * 取得本季度剩余天数
	 * @param fianlDate
	 * @return
	 */
	public static int getRemainDayOfQuarter() {
		Date startDate = new Date();
//		if (fianlDate != null)
//			startDate = new DateTime(fianlDate).toDate();
		try {
			return differWorkingDay(startDate, getCurrentQuarterEndTime());
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 当前季度的结束时间。
	 * @return
	 */
	public static Date getCurrentQuarterEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getCurrentQuarterStartTime());
		cal.add(Calendar.MONTH, 3);
		return cal.getTime();
	}
	
	/**
	 * 当前季度的开始时间。
	 * @return
	 */
	public static Date getCurrentQuarterStartTime() {
		int nowMonth = DateTime.now().getMonthOfYear();
		int quarter = getQuarter(nowMonth);
		int startMonth = quarter * 3 - (3 - 1);
		return new DateTime(DateTime.now().getYear()+"-"+startMonth).toDate();
	}
	
	/**
	 * 取两个日期，相隔的天数
	 * @param stime
	 * @param etime
	 * @throws Exception 
	 */
	public static int differDay(Date stime,Date etime){
		long st = stime.getTime();
		long et = etime.getTime();
		long between_days = (et - st) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}
	
	/**
	 * 取两个日期，相隔的工作日天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int differWorkingDay(Date startDate, Date endDate) {
		Calendar calendar = Calendar.getInstance();
		int result = 0;
		while (startDate.compareTo(endDate) <= 0) {
			calendar.setTime(startDate);
			if (isWorkDay(calendar))
				result++;
			calendar.add(Calendar.DATE, 1);
			startDate = calendar.getTime();
		}
		return result;
	}
	
	  // 判断是否工作日（未排除法定节假日，由于涉及到农历节日，处理很麻烦）
    public static boolean isWorkDay(Calendar c) {
        // 获取星期,1~7,其中1代表星期日，2代表星期一 ... 7代表星期六
        int week = c.get(Calendar.DAY_OF_WEEK);
        // 不是周六和周日的都认为是工作日
        return week != Calendar.SUNDAY && week != Calendar.SATURDAY;
    }
    
}
